// Device/pages/Info/components/alertor-component/index.js
const presenceState = {
    0: '进入',
    1: '跌倒',
    3: '离开',
    5: '滞留'
}
const deviceStatus = {
    '0': '离线',
    '1': '在线'
}
Component({
    /**
     * 组件的初始数据
     */
    data: {
        deviceData: []
    },
    /**
     * 组件的属性列表
     */
    properties: {
        deviceInfo: {
            type: Object,
            value: {}
        }
    },
    observers: {
        deviceInfo(value) {
            const data = new Map([
                ['device_status', '联网状态'],
                ['signal_strength', '信号强度']
            ]);
            let obj = {
                device_status: deviceStatus[value['device_status']],
                signal_strength: value['signal_strength'] ? `${value['signal_strength']}dBm` : '--'
            }
            let result = [];
            for (let [key, value] of Object.entries(obj)) {
                result.push({
                    key: data.get(key),
                    value,
                    path: '/Device/pages/Analysis/index?type=' + key
                })
            }
            let callList = value.sos_call_info.map((item) => {  
                return {  
                    time: item.create_time,  
                    state: item.call_type,  
                    result: item.call_result  
                }  
            }) 
            if (value.device_type == '6') {
                this.setData({
                    deviceList: value.device_bases?.map((item) => {
                        return {
                            ...item,
                            device_imei: item.device_imei.split('-')[1]
                        }
                    })
                })
            }
            this.setData({
                deviceData: result,
                callList
            })
        }
    },



    /**
     * 组件的方法列表
     */
    methods: {
        onUnpair(e) {
            const that = this;
            wx.showModal({
                title: '确认操作？',
                content: '是否确认解除所有配对设备？',
                complete: (res) => {
                    if(res.confirm) {
                        that.triggerEvent('unpair', { device_imei: e.currentTarget.dataset.imei })
                    }
                }
            })
        },
        onChangeName(e) {
            const that = this;
            wx.showModal({
                title: '修改设备名称',
                content: e.currentTarget.dataset.name,
                editable: true,
                complete: (res) => {
                    if (res.confirm) {
                        that.triggerEvent('update', {
                            device_imei: e.currentTarget.dataset.imei,
                            device_name: res.content
                        })
                    }
                }
            })
        }
    }
})